package com.duobao.server;

import java.util.Vector;

import com.duobao.uitl.Formula;
import com.duobao.uitl.Gaili;
import com.duobao.uitl.ZhXiuwei;
import com.duobao.uitl.Zuanhuan;
import com.duobao.wuping.Hunmi;
import com.duobao.wuping.Shiti;

public class ZhangDou implements Runnable
{
    NPC npc;//被打的
    Vector<NPC> npcs;//殴打的
    int  map;//战斗所在地图

    Vector<String> strs;

    int zhaoshi;//hero的出招
    boolean taopao;//hero逃跑还是战斗     TRUE逃跑， false战斗
    public ZhangDou(NPC npc,Vector<NPC> npcs,int map)
    {
        this.npc=npc;
        this.npcs=npcs;


        this.map=map;
        this.zhaoshi=1;
        strs=new Vector<String>();
        this.strs.add("你目不转睛的盯着对方");
    }


    public boolean isTaopao() {
        return taopao;
    }


    public void setTaopao(boolean taopao) {
        this.taopao = taopao;
    }


    public NPC getNpc() {
        return npc;
    }

    public void setNpc(NPC npc) {
        this.npc = npc;
    }


    public Vector<NPC> getNpcs() {
        return npcs;
    }

    public void setNpcs(Vector<NPC> npcs) {
        this.npcs = npcs;
    }

    public String[] getStrs() {
        String ss[] = null;
        if(this.strs.size()==0)
        {
            ss=new String[]{"你缓缓的移动脚步，寻找对方的破绽。"};
        }else
        {
            ss=new String[this.strs.size()];
            for(int i=0;i<this.strs.size();i++)
            {
                ss[i]=this.strs.get(i);;

            }
            this.strs.removeAllElements();

        }
        return ss;
    }
    public String[] mapmiaoshu() {
        String ss[] = null;

        ss=new String[this.strs.size()];
        for(int i=0;i<this.strs.size();i++)
        {
            ss[i]=this.strs.get(i);;

        }
        return ss;
    }
    public void setStrs(String str) {
        if(this.strs.size()>4)
        {
            this.strs.remove(0);
            this.strs.add(str);
        }else
        {
            this.strs.add(str);
        }
    }
    public void diaohuan()//调换
    {
        NPC npc=this.npc;
        this.npc=this.npcs.get(0);
        this.npcs.remove(0);
        this.npcs.add(npc);
    }
    public int getZhaoshi() {
        return zhaoshi;
    }

    public void setZhaoshi(int zhaoshi) {
        this.zhaoshi = zhaoshi;
    }
    public void hunmi(NPC npc1)//昏迷
    {
        setStrs(npc1.name+"失去了知觉");
        Map map=Maps.getMap(npc1.getLocation());
        map.getNpcs().remove(npc1);
        Hunmi e=new Hunmi(npc1.getName(),map.getEquips(),npc1);
        e.setLocation(npc1.getLocation());
        map.getEquips().add(e);
        npc1.ee=e;
        Thread th=new Thread(e);
        th.start();
    }
    private void mingzhoushouhuo(NPC npc1,NPC npc2,WuGong chu1)//命中未死收获
    {
        npc1.setCultivationY(npc1.getCultivationY()+1);
        npc1.setChengWei();

        //修为小于对方的修为乘1.2，就有潜能
        if(npc1.getCultivationY()<=(int)(npc2.getCultivationY()*1.2f))
        {
            if(npc1.leib==2)
            {
                setStrs("你的修为增加了！你的潜能上升了！");
            }
            npc1.addcultivation(0, 1, 0);
            npc1.addProficiency(1);
            chu1.addBengjijingyan(1);
        }



    }
    public void herosw(NPC npc1,NPC npc2)//npc1被npc2杀死
    {
        Float xiuwei=Formula.diushixiuwei(npc1);
        int nian=xiuwei.intValue();
        Float tianf=((xiuwei-nian)*250);
        int tian=tianf.intValue();
        Float shic=(tianf-tian)*12;
        int sc=shic.intValue();
        System.out.println("nian:"+nian+"    tian:"+tian+"    sc:"+sc);
        npc1.addcultivation(-nian,-tian,-sc);
        npc1.setXiuWei();
        if(npc1.leib==2)
        {
            setStrs("你被"+npc2.getName()+"杀死了，损失了"+ZhXiuwei.xwei(nian, tian, sc)+"修行。");
        }

    }
    public void swhoude(NPC npc1,NPC npc2)//npc1杀死npc2死亡后获得
    {
        int npc2_maxwg=npc2.getMaxWg();
        if(npc1.getCultivationN()<(npc2_maxwg+11)*(npc2_maxwg+11)*(npc2_maxwg+11)/1000)
        {
            int qneng=(npc2_maxwg/5==0?1:npc2_maxwg/5);
            npc1.addProficiency(qneng);

            Float xiuwei=Formula.houdexiuwei(npc2);
            int nian=xiuwei.intValue();
            Float tianf=((xiuwei-nian)*250);
            int tian=tianf.intValue();
            Float shic=(tianf-tian)*12;
            int sc=shic.intValue();

            System.out.println("npc1:"+npc1.getName()+"    xiuwei:"+xiuwei+"    nian:"+nian+"    tian:"+tian+"    sc:"+sc);
            npc1.addcultivation(nian,tian,sc);
            npc1.setXiuWei();
            if(npc1.leib==2)
            {
                setStrs("你杀死了"+npc2.getName()+"，得到"+ZhXiuwei.xwei(nian, tian, sc)+"修行，"+Zuanhuan.zhuang(qneng)+"点潜能。");
                //System.out.println(("你杀死了"+npc2.getName()+"，得到"+ZhXiuwei.xwei(nian, tian, sc)+"修行，"+qneng+"点潜能。"));
            }

        }
        if(npc1.leib==2)
        {
            setStrs("你杀死了"+npc2.getName()+"。");
        }


    }
    public void shiwangPanding2(NPC npc1,NPC npc2)//npc1杀死npc2 死亡判定2;
    {
        Map map=Maps.getMap(npc2.getLocation());
        map.getNpcs().remove(npc2);
        npc2.setSwTime(System.currentTimeMillis());
        if(npc2.leib==2)//玩家死亡
        {
            Map map2=Maps.getMap(11);
            map2.getNpcs().add(npc2);
            npc2.hufouhuofu();
            this.herosw(npc2,npc1);
            this.swhoude(npc1, npc2);
            npc2.setLocation(11);
        }else
        {
            Maps.df.getNpcs().add(npc2);
            //npc2.setLocation(-1);
            npc2.siwang=true;
            this.swhoude(npc1, npc2);
        }
        if(npc2.ee!=null)
        {
            Hunmi hunmi=(Hunmi) npc2.ee;
            hunmi.siwang();
        }
        Shiti eee=new Shiti(npc2.getName(),map.getEquips());
        eee.setDQTime(System.currentTimeMillis());
        Thread th=new Thread(eee);
        th.start();
        map.getEquips().add(eee);

        int size=npc2.getEquip().size();
        for(int i=0;i<size;i++)
        {
            Equip  e=npc2.getEquip().get(size-i-1);
            npc2.getEquip().remove(e);
            e.setZB(false);
        }
        int size2=npc2.getEquip2().size();
        for(int i=0;i<size2;i++)
        {
            Equip  e=npc2.getEquip2().get(size2-i-1);
            npc2.getEquip2().remove(e);
            map.getEquips().add(e);
        }
        npc2.ZD=null;
        npc2.setFZ();
    }
    public void shiwangPanding1(NPC npc1,NPC npc2)//npc1杀死npc2 死亡判定1;
    {
        if(npc2==this.npc)//主NPC
        {

        }else
        {
            this.npcs.remove(npc2);
        }
        shiwangPanding2(npc1, npc2);
    }
    public void end()
    {
        this.npc.ZD=null;

        for(int i=0;i<this.npcs.size();i++)//剩下的也设为空
        {
            this.npcs.get(i).ZD=null;
        }
    }
    public void mingzhonog(NPC npc1,NPC npc2,WuGong chu1,int chuhsou,int chuzhao)//命中npc2
    {
        int zhshai=Formula.shanghaiZh(npc1, npc2, chuhsou);
        npc2.HP=npc2.HP-zhshai;
        System.out.println("伤害："+zhshai+"    HP:"+npc2.HP);
        System.out.println("zhshai:"+zhshai);
        npc2.setQxue();
        npc2.setXiuWei();

        String str1=chu1.getchuzhao(chuzhao, npc1, npc2)+chu1.getzhongzhao(chuzhao, npc1, npc2)+npc2.getName()+"看起来"+npc2.getQxue()+"！";
        setStrs(str1);

        if(npc2.HP<0)//死亡
        {
            this.shiwangPanding1(npc1,npc2);
        }else if(npc2.HP<(npc2.MaxHP*0.1f) && !npc2.hunmi)//昏迷
        {
            this.mingzhoushouhuo(npc1, npc2, chu1);
            this.hunmi(npc2);
        }else
        {
            this.mingzhoushouhuo(npc1, npc2, chu1);
        }

    }
    public int chuzhaoxiaohao(NPC npc1,WuGong chu)//出招消耗
    {
        return 0;
    }
    public int gjichuzhao(NPC npc1,WuGong chu1)//npc1攻击出招
    {
        int npc1_zhaoshi=0;
        if(npc1.leib==2)
        {
            npc1_zhaoshi=this.zhaoshi;
        }else
        {
            npc1_zhaoshi=(int)(Math.random()*chu1.tezhaoshu)+1;
        }
        chuzhaoxiaohao(npc1, chu1);
        return npc1_zhaoshi;
    }
    public int zjiachuzhao(NPC npc1,WuGong chu1)//npc1招架出招
    {
        int npc1_zhaoshi=0;
        npc1_zhaoshi=(int)(Math.random()*chu1.zjiashu)+1;
        chuzhaoxiaohao(npc1, chu1);
        //System.out.println("npc1_zhaoshi:"+npc1_zhaoshi);
        return npc1_zhaoshi;
    }
    public int qgongchuzhao(NPC npc1,WuGong chu1)//npc1轻功出招
    {
        int npc1_zhaoshi=0;
        npc1_zhaoshi=(int)(Math.random()*chu1.tezhaoshu)+1;
        chuzhaoxiaohao(npc1, chu1);
        return npc1_zhaoshi;
    }
    public void chushou(NPC npc1,NPC npc2)//npc1攻击
    {
        //设置npc1
        WuGong chu1=npc1.chushouWugong();//出手武功
        int npc1_zhaoshi=gjichuzhao(npc1, chu1);//出招
        int chuZHe1=Formula.chushouZh(npc1,chu1,npc1_zhaoshi);//出招综合数



        //设置npc2
        WuGong chu2=npc2.zaojiaWugong();//招架
        //System.out.println(chu2.getName());
        WuGong qing2=npc2.duobiWugong();//轻功
        int npc2_zhaoshi=gjichuzhao(npc2, qing2);//轻功出招
        int qingZHe2=Formula.chushouZh(npc2,qing2,npc2_zhaoshi);//出招综合数

        System.out.println("npc1:"+npc1.name+"    chuZHe1:"+chuZHe1+"    qingZHe2:"+qingZHe2);
        if(Gaili.zhangdou(chuZHe1, qingZHe2))//破了轻功
        {

            if(chu2.zjiashu>0)//武功有招架招式
            {
                int npc2_zhaojia=zjiachuzhao(npc2, chu2);
                int chuZHe2=Formula.zhaojiaZh(npc2, chu2, npc2_zhaojia);
                if(Gaili.zhangdou(chuZHe1,chuZHe2))//破了招架
                {
                    this.mingzhonog(npc1,npc2,chu1,chuZHe1,npc1_zhaoshi);
                }else
                {
                    //System.out.println("chu2:"+chu2.getName());
                    //System.out.println(chu2.getzhaojia(npc2_zhaojia, npc2, npc1)+"    npc2_zhaojia:"+npc2_zhaojia);
                    setStrs(chu1.getchuzhao(npc1_zhaoshi, npc1, npc2)+chu2.getzhaojia(npc2_zhaojia, npc2, npc1));
                }
            }else
            {
                this.mingzhonog(npc1,npc2,chu1,chuZHe1,npc1_zhaoshi);
            }
        }else
        {
            setStrs(chu1.getchuzhao(npc1_zhaoshi, npc1, npc2)+qing2.getchuzhao(npc2_zhaoshi, npc2, npc1));
        }
    }
    public void taopa(NPC npc1,NPC npc2)//npc1逃跑
    {

        int qingZHe1=Formula.taopaoZh(npc1);
        if(npc1==npc)//主NPC逃跑要求比较所有NPC的轻功
        {
            int Max_qing=0;
            String name="";
            for(int i=0;i<this.npcs.size();i++)
            {

                int qingZHe=Formula.taopaoZh(this.npcs.get(i));
                if(qingZHe>Max_qing)
                {
                    Max_qing=qingZHe;
                    name=this.npcs.get(i).getName();
                }
            }
            System.out.println("npc1:"+npc1.getName()+"    qingZHe1:"+qingZHe1+"    Max_qing:"+Max_qing);
            if(Gaili.taopao(qingZHe1, Max_qing))
            {
                this.strs.add(npc1.name+"逃跑了");
                npc1.ZD=null;
                npc1.taopao();
            }else
            {
                setStrs(npc1.name+"转身欲走，被"+name+"一把拦住，想走？没那么容易！");
            }
        }else
        {
            int qingZHe2=Formula.taopaoZh(npc2);

            System.out.println("npc1:"+npc1.getName()+"    qingZHe1:"+qingZHe1+"    qingZHe2:"+qingZHe2);
            if(Gaili.taopao(qingZHe1, qingZHe2))
            {
                this.strs.add(npc1.name+"逃跑了");
                npc1.ZD=null;
                npc1.taopao();
                this.npcs.remove(npc1);
            }else
            {
                setStrs(npc1.name+"转身欲走，被"+npc2.name+"一把拦住，想走？没那么容易！");
            }

        }


    }
    public void xingdong(NPC npc1,NPC npc2)//
    {
        if(npc1.leib==2)//是玩家
        {
            if(npc1.HP<(npc1.MaxHP*0.1f))
            {
                //昏迷了不行动
            }else if(this.taopao)//逃跑
            {
                taopa(npc1, npc2);
                this.taopao=false;//
            }else//出手攻击
            {
                chushou(npc1, npc2);
            }
        }else//系统NPC
        {
            if(npc1.HP<(npc1.MaxHP*0.1f))
            {
                //昏迷了不行动
            }else if(npc1.HP<(npc1.MaxHP*0.3f))//命旦自动逃跑
            {
                taopa(npc1, npc2);
            }else//出手攻击
            {
                chushou(npc1, npc2);
            }
        }
    }
    public void run() {
        // TODO Auto-generated method stub
        while(true)
        {


            if(this.npc.getLocation()==this.map && this.npcs.size()>0)//判断主NPC是否还在地图上
            {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                this.xingdong(this.npc,this.npcs.get((int)(Math.random()*this.npcs.size())));
            }else
            {
                end();
                break;
            }
            for(int i=0;i<this.npcs.size();i++)
            {
                this.xingdong(this.npcs.get(i),this.npc);
            }
            this.taopao=false;
        }
    }
}
